package com.zhanglijie.theory.immutable.effective;

import sun.java2d.loops.ProcessPath;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/7/26 0026 23:29
 */
public final class Line {
    final Point start;
    final Point end;

    /**
     * 这个禁止使用这个构造函数  方式再此被修改start  end
     * @param start
     * @param end
     */
    public Line(Point start, Point end) {
        // this.start = start;
        // this.end = end;  或者改成下面的  才是真正的immutable模式  ,因为 start end这个引用变量可能在外部被修改
        //主要外部set的时候也要加sunc锁 而且set必须是在同一个sycn一部set修改

        synchronized (start){
            this.start = new Point(start.x,start.y);
        }
        synchronized (end){
            this.end = new Point(end.x,end.y);
        }
    }

    public Line(final int startX,final int startY,final int endX,final int endY){
        this.start = new Point(startX,startY);
        this.end = new Point(endX,endY);
    }

    public int getStartX(){
        return start.x;
    }
    public int getStartY(){
        return start.y;
    }
    public int getEndX(){
        return end.x;
    }
    public int getEndY(){
        return end.y;
    }

    @Override
    public String toString() {
        return "Line{" + "start=" + start + ", end=" + end + '}';
    }
}
